package com.chen.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.User;
import com.chen.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.Result;
import com.chen.service.IVideoService;
import com.chen.entity.Video;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y·C
 * @since 2023-04-26
 */
@Api(tags = "视频接口")
@RestController
@RequestMapping("/video")
public class VideoController {

    @Resource
    private IVideoService videoService;


    @ApiOperation("视频新增或者更新")
    @PostMapping
    public Result save(@RequestBody Video video) {

        if (video.getId()== null){
        User user = TokenUtils.getCurrentUser();
        video.setUsername(user.getUsername());
        }
        videoService.saveOrUpdate(video);

        return Result.success();
    }


    @ApiOperation("删除一条或者多条视频")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        videoService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("显示全部")
    @GetMapping()
    public Result findAll() {
        return Result.success(videoService.list());
    }


    @ApiOperation("条件（按照用户名）查询分页显示全部")
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String username) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.like("username",username);
        return Result.success(videoService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    @ApiOperation("视频审核接口 一条或多条")
    @PostMapping("/audit")
    public Result audit(@RequestBody List<Integer> ids){
        videoService.adoptByCids(ids);
        return Result.success();
    }


}

